﻿using sonic.Samples.Infrastructure.ViewModels;
using sonic.Samples.Infrastructure.Services;
using sonic.Samples.Infrastructure.Model;
using Microsoft.Practices.Composite.Events;
using sonic.Samples.Infrastructure.Events;

namespace Modules.Bing
{
    public class MapViewModel : ViewModelBase
    {
        private IAccountService accountService;
        private IEventAggregator eventAggregator;
        public MapViewModel(IAccountService accountService, IEventAggregator eventAggregator)
            : base()
        {
            this.DisplayName = "This is the Map";
            this.accountService = accountService;
            this.eventAggregator = eventAggregator;
            eventAggregator.GetEvent<CurrentAccountEvent>().Subscribe(CurrentAccountChanged);
        }

        public Account CurrentAccount
        {
            get {
                return this.accountService.GetCurrent();
            }
            set{
                this.accountService.SetCurrent(value.LastName);
                NotifyPropertyChanged("CurrentAccount");
                NotifyPropertyChanged("CurrentLocation");

            }
        }

        public Microsoft.Maps.MapControl.Location CurrentLocation
        {
            get {
                if (this.CurrentAccount == null) return new Microsoft.Maps.MapControl.Location(0, 0);
                return new Microsoft.Maps.MapControl.Location(this.CurrentAccount.CurrentLocation.Latitude, this.CurrentAccount.CurrentLocation.Longitude); 
            }

        }

        public void CurrentAccountChanged(Account o)
        {
            CurrentAccount = o;
        }
    }
}
